﻿using IBLL;
using IDAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class StoreBLL:IStoreBLL
    {
        public StoreBLL(IBaseDAL<Store> storeDAL,IBaseDAL<Address> addressDAL)
        {
            StoreDAL = storeDAL;
            AddressDAL = addressDAL;
        }

        public IBaseDAL<Store> StoreDAL { get; }
        public IBaseDAL<Address> AddressDAL { get; }

        public async Task<int> AddStore(Store store)
        {
            var a = StoreDAL.GetAllAsync().Any(x => x.StoreName == store.StoreName);//判断门店名称唯一性
            if(!a)
            {
                return await StoreDAL.AddAsync(store);
            }
            else
            {
                return -2;//门店名称不唯一
            }
            
        }

        public dynamic ShowStore(int aid, DateTime? time, int start, string? name = "")
        {
            var Store = StoreDAL.GetAllAsync();
            var Address= AddressDAL.GetAllAsync();
            var list = from a in Store
                       join b in Address
                       on a.AreaId equals b.AddressId
                       select new
                       {
                           a.StoreName,
                           a.Manager,
                           a.Phone,
                           a.CreateDate,
                           a.MaxPeople,
                           a.Start,
                           a.AreaId,
                           a.CrateTime,
                           b.AddressId,
                           b.AddressName,

                       };
            if(aid>0)
            {
                list=list.Where(x=>x.AddressId==aid);
            }
            if(time!=null)
            {
                var A = time.Value.AddDays(1);
                list = list.Where(x => x.CreateDate >= time && x.CreateDate < A);
            }
            if(start>0)
            {
                list = list.Where(x => x.Start == start);
            }
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.Manager.Contains(name));
            }
            return list.ToList();
        }
    }
}
